/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: Frank
 * 系统版本: 1.0.0
 * 开发人员: Frank
 * 开发时间: 2019/12/10 11:34
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.uaa.model;

import com.spring.arch.common.enums.Gender;
import com.spring.arch.common.enums.UserStatus;
import com.spring.arch.common.jpa.model.AbstractAuditableModel;
import com.spring.arch.common.validator.constraints.Mobile;
import com.spring.arch.common.enums.Gender;
import com.spring.arch.common.enums.UserStatus;
import com.spring.arch.common.jpa.model.AbstractAuditableModel;
import com.spring.arch.common.validator.constraints.Mobile;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * 账户信息，账号统一管理
 *
 * @author Frank
 * @version 1.0.0.1
 * @since 2019/12/10 11:34
 */
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "user")
public class User extends AbstractAuditableModel {

    private static final long serialVersionUID = 7347393054885624202L;

    /**
     * 手机号（登录主要凭证）
     */
    @NotBlank
    @Size(max = 11)
    @Mobile
    @Column(nullable = false, unique = true)
    private String mobile;

    /**
     * 账号（登录凭证）
     */
    @Size(max = 32)
    @Column(unique = true)
    private String account;

    /**
     * 真实姓名
     */
    @NotBlank
    @Size(max = 32)
    private String realName;

    /**
     * 密码
     */
    @NotBlank
    @Size(max = 64)
    @Column(nullable = false)
    private String password;

    /**
     * 密码盐
     */
    @NotBlank
    @Size(max = 16)
    @Column(nullable = false)
    private String salt;

    /**
     * 邮箱
     */
    @Size(max = 32)
    @Email
    private String email;

    /**
     * 性别
     */
    @Enumerated(EnumType.STRING)
    @Column(length = 32)
    private Gender gender;

    /**
     * 身份证号
     */
    @Size(max = 64)
    private String identityNo;

    /**
     * 用户状态
     */
    @NotNull
    @Column(length = 32)
    private UserStatus status = UserStatus.ACTIVE;

    public User(@NotBlank @Size(max = 11) String mobile,
                @Size(max = 32) String account,
                @Size(max = 32) String realName,
                @NotBlank @Size(max = 64) String password,
                @NotBlank @Size(max = 16) String salt,
                @Size(max = 32) @Email String email,
                Gender gender,
                @Size(max = 64) String identityNo) {
        this.mobile = mobile;
        this.account = account;
        this.realName = realName;
        this.password = password;
        this.salt = salt;
        this.email = email;
        this.gender = gender;
        this.identityNo = identityNo;
    }

    public boolean isEnabled() {
        return status == UserStatus.ACTIVE
                || status == UserStatus.CREATED
                || status == UserStatus.INACTIVE
                || status == UserStatus.TEMP_LOCKED_BAD_CREDENTIALS;
    }
}
